package com.yanqingshan.platform.module.sys.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.yanqingshan.platform.common.constant.AppConstant;
import com.yanqingshan.platform.common.core.domain.PageQuery;
import com.yanqingshan.platform.common.core.domain.R;
import com.yanqingshan.platform.module.sys.convert.SysUserConvert;
import com.yanqingshan.platform.module.sys.model.domain.SysRole;
import com.yanqingshan.platform.module.sys.model.domain.SysUser;
import com.yanqingshan.platform.module.sys.model.vo.*;
import com.yanqingshan.platform.module.sys.service.SysResourceService;
import com.yanqingshan.platform.module.sys.service.SysRoleService;
import com.yanqingshan.platform.module.sys.service.SysUserRoleService;
import com.yanqingshan.platform.module.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述: 系统用户 前端控制器
 *
 * @author yanqs
 * @since 2023-05-12
 */
@Api(tags = "系统用户")
@ApiSupport(order = 2)
@RestController
@RequestMapping(AppConstant.APPLICATION_SYSTEM + "/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysResourceService resourceService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("创建用户")
    @PostMapping("/create")
    public R createUser(@Valid @RequestBody SysUserReqVO param) {
        return sysUserService.createUser(param);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("更新用户资料")
    @PutMapping("/update")
    public R updateUser(@Valid @RequestBody SysUserReqVO param) {
        return sysUserService.updateUser(param);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("修改用户密码")
    @PostMapping("/change-password")
    public R changePassword(@RequestBody @Valid ChangePasswordReqVO param) {
        return sysUserService.changePassword(param);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public R delUser(@PathVariable Long id) {
        return sysUserService.delUser(id);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("获取用户信息")
    @PostMapping("/info")
    public R<CurrentUserInfoRespVO> getUserInfo() {
        //获取当前登录用户的用户ID
        if (Objects.isNull(StpUtil.getLoginIdDefaultNull())) {
            return R.failed("当前用户未登录");
        }
        Long userId = StpUtil.getLoginIdAsLong();
        SysUser user = sysUserService.getById(userId);
        SysUserInfoRespVO userInfo = SysUserConvert.INSTANCE.convert(user);
        CurrentUserInfoRespVO currentUserInfoRespVO = resourceService.listForLogin(user.getId());
        currentUserInfoRespVO.setUserInfo(userInfo);
        currentUserInfoRespVO.setPermissions(resourceService.listAllPermissionsByUserId(user.getId()));
        return R.ok(currentUserInfoRespVO);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("用户分页")
    @GetMapping("/page")
    public R pageCustom(SysUserReqVO param, PageQuery query) {
        return R.ok(sysUserService.pageCustom(param, query));
    }


    @ApiOperationSupport(order = 7)
    @ApiOperation("获取用户详细信息")
    @GetMapping("/{id}")
    public R<SysUserDetailsRespVO> getUserDetails(@PathVariable Long id) {
        return R.ok(sysUserService.getUserDetails(id));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("验证用户是否唯一")
    @GetMapping("/validate-username")
    public R validateUserName(String userName, Long userId) {
        return R.ok(sysUserService.validateUserName(userName, userId));
    }

    /**
     * 查询用户待分配角色
     *
     * @return
     */
    @GetMapping("/query-grant-roles")
    public R<List<SysRole>> queryGrantRolesByUserId(@RequestParam Long userId) {
        return R.ok(sysUserService.queryGrantRolesByUserId(userId));
    }

    /**
     * 查询用户分配角色
     *
     * @return
     */
    @GetMapping("/query-roles")
    public R<List<SysRole>> queryRolesByUserId(@RequestParam Long userId) {
        return R.ok(sysUserService.queryRolesByUserId(userId));
    }

    /**
     * 选择角色
     *
     * @param userId  用户id
     * @param roleIds 角色id
     * @return
     */
    @PostMapping("/grant-select-role")
    public R grantSelectRole(@RequestParam Long userId, @RequestParam List<Long> roleIds) {
        Assert.notNull(userId, "人员 ID 不能为空");
        Assert.notEmpty(roleIds, "角色 ID 不能为空");
        return R.status(sysUserRoleService.grantSelectRole(userId, roleIds));
    }

    @PostMapping("/cancel-role")
    public R cancelRole(@RequestParam Long userId, @RequestParam List<Long> roleIds) {
        Assert.notNull(userId, "人员 ID 不能为空");
        Assert.notEmpty(roleIds, "角色 ID 不能为空");
        return R.status(sysUserRoleService.cancelRole(userId, roleIds));
    }

    /**
     * 重置密码
     * @param userIds
     * @param password
     * @return
     */
    @PostMapping("/reset-password")
    public R resetPassword(@RequestParam List<Long> userIds, @RequestParam String password) {
        Assert.notEmpty(userIds, "用户 ID 不能为空");
        Assert.notEmpty(password, "密码字段不能为空");
        return R.status(sysUserService.resetPassword(userIds, password));
    }
}
